package day11.Object;

/**
 * @Auther:小陈的Java
 */
public class Product1 implements Cloneable{
    //产品id
    private int id;
    //产品名字
    private String name;
    //产品分类信息
    private Category1 category1;


    public Product1(int id, String name, Category1 category1) {
        this.id = id;
        this.name = name;
        this.category1 = category1;
    }

    public Product1() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category1 getCategory1() {
        return category1;
    }

    public void setCategory1(Category1 category1) {
        this.category1 = category1;
    }

    @Override
    public String toString() {
        return "Product1{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", category1=" + category1 +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //拷贝产品类
        Product1 product1 = (Product1) super.clone();
        //将产品类的分类属性拷贝一份，赋值到拷贝后的产品类
        product1.category1 = (Category1) category1.clone();
        return product1;
    }

    /*程序入口*/
    public static void main(String[] args)  throws Exception{
        //创建产品分类对象，产品对象，并将产品分类对象注入产品对象中
        Category1 category1 = new Category1(1,"家电");
        Product1 product1 = new Product1(1,"联想笔记本",category1);
        //浅拷贝产品对象
        Product1 product2 = (Product1) product1.clone();
        //改变拷贝后的对象的引用类型属性的内容
        product2.getCategory1().setName("笔记本");
        //拷贝前的对象的引用类型属性不受拷贝后对象的影响
        System.out.println(product2);
        System.out.println(product1);
    }
}
//产品分类
class Category1 implements Cloneable{
    //分类id
    private int id;
    //分类名字
    private String name;

    public Category1(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Category1() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Category1{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
